extends CanvasLayer


@onready var player_health_bar: ProgressBar = $PlayerHealthBar
@onready var player_health_label: Label = $PlayerHealthBar/PlayerHealthLabel
@onready var time_lbl: Label = $TimeLbl
@onready var score_lbl: Label = $ScoreLbl
@onready var kill_count_lbl: Label = $KillCountLbl
@onready var player: Player = get_tree().get_first_node_in_group("player")

var over_time: int


func _ready() -> void:
    player.health_changed.connect(
        func():
            var tween := get_tree().create_tween()
            player_health_label.text = "%d/%d" % [player.health, player.max_health]
            tween.tween_property(player_health_bar, "value", player.health, 0.25)
            tween.play()
    )
    
    player.max_health_changed.connect(
        func():
            player_health_bar.max_value = player.max_health
            player_health_label.text = "%d/%d" % [player.health, player.max_health]
    )
    
    player.score_changed.connect(
        func():
            score_lbl.text = "分数：%d" % player.score
    )
    
    player.kill_count_changed.connect(
        func():
            kill_count_lbl.text = "击杀数：%d " % player.kill_count
    )
    
    player_health_bar.max_value = player.max_health
    player_health_bar.value = player.health
    player_health_label.text = "%d/%d" % [player.health, player.max_health]


func _process(delta: float) -> void:
    time_lbl.text = _convert_time((TimeManager.get_playing_ticks_msec() - over_time) / 1000)


func _convert_time(time: int) -> String:
        var minutes = time / 60
        var seconds = time % 60
        return "%02d:%02d" % [minutes, seconds]
